<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>CS3340: Windows Programming</title><!--

	Grading: Read code first
--></head><body>

<center><h3> Program 7: Threading (20 points)
            <br>
            Due: 11 PM, Thursday, April 26 2007
</h3></center>

The program consists of two projects: UserClass and Prog7.

<h3> Project UserClass     </h3>
<ol>
     <li> This is a Class Library project. The assembly name <b>must</b> be UserClass, 
            and the root namespace <b>must</b>
         be UWPCS3340. The project defines a public enumeration type UserState, two public delegates, and a 
         public class UserAccount.</li>
     <li> The enumeration type UserState should be defined outside of class UserAccount 
            and has five possible values:
<pre>   Started     Working     Waiting     Terminating      Terminated
</pre></li>
      <li> The two delegates are 
<pre>    Public Delegate Sub Transaction(ByVal ID As String, ByVal value As Integer, ByVal Final As Boolean)
    Public Delegate Sub PassMsg(ByVal myID As String, ByVal theState As UserState)
</pre>      
      </li>
      <li> The class UserAccount represents users sharing one common bank account. After 
         started, a user goes to work and issues transactions. A user may be suspended 
         for some reason. If suspended, a user should complete the current work and 
         transaction and then wait. A waiting user may be resumed or terminated later. A 
         user can be terminated at any time, but a working user should finish the 
         current work and transaction before being terminated.</li>
      <li> The class should have the following private members:
            <pre>   Private userThread As Threading.Thread
   Private Paused As New ManualResetEvent(False)
   Private _state As UserState
   Private myGenerator As Random
</pre></li>
       <li> The Interface of the class is defined below:
            <ul>
            <li> Public members
            <ul>
                <li> DoTransaction: delegate Transaction, invoked to issue each transaction and
                     the Final transaction amount. </li>
                <li> Report: delegate PassMsg, invoked to report the user state. </li>
            </ul>
            </li>
            <li> Property
                 <ul> 
                 <li> ID: string, read only, returns the GetHashCode value of the user thread.
    You must put two-characters initials of one student in front of the ID. </li>
                <li> MainForm: used to invoke the two delegates. </li>
                </ul></li>
            <li> Methods
                  <ul>   
                  <li> Sub SpinUp: set the user state to Start and start the user thread </li>
                  <li> Sub SpinDown: terminate the user thread, wait up to 5 seconds for the user thread to terminate,
                                    make sure to wake up the user thread if it is in state waiting. </li>
                  <li> Sub UserStop: suspend the user thread. </li>
                  <li> Sub UserContinue: resume the user thread. </li>
   </ul></li>
            <li> You must not use methods Abort, Interrupt, Suspend or Resume of Thread. You will <b>lose 3 
                     points</b> for each such method used in your code.</li></ul>
         </li>
      <li> The class should have a private Sub Run for the userThread to execute. The 
            pseudocode of the Sub is given below:
<pre>      Report user state
      Set seed for myGenerator
      While True
         If user state is Waiting
            Report user state
            Wait until being resumed or terminated

         If in state Terminating
            Report user state
            Exit loop

         Report user state
         Go to work for some time 
         Generate a transaction amount
         Issue a transaction of the amount
      End While

      Report user state being terminated
      Issue total transaction
</pre></li>
      <li> For each iteration of the while loop, the user will work (go to sleep) for a 
         while, then generate a value and issue a transaction. You must use a random 
         generator (class Random) to decide the length of working time 
         and the amount of each transaction. You can use the seconds value 
         of the current time (function Now()) as the seed for the random generator when the userThread started. 
         The working time is between 3 and 5 seconds, and the transaction amount is 
         between -100 and 100.</li>
       <li> Each user should keep its own total amount of transactions and report the 
         amount when terminating before going out of Sub Run.</li>
       <li> You can use either a ManualResetEvent object or AutoResetEvent object to handle 
         the Waiting state.</li>
</ol>
      
<h3> Project Prog7 </h3>
<ol>
     <li> This is the GUI Front-End program. </li>
     <li> The program has one form and your form should be similar to the form of the sample program.</li>
     <li> The form has the following controls:
          <ul>
          <li> TextBox txtBalance: To display the balance of the common account.</li>
          <li> TextBox txtTransaction: To display the sum of total transactions for all users.</li>
          <li> TextBox txtLog: To keep trak of all individual transactions, also to record the 
                     start and termination of each user.</li>
          <li> ListBox lstAllUsers: To display all users with their current state and the 
                        total transaction amount afetr each user is terminated.</li>
          <li> Command button cmdCreate: To start a new user, which will be added to the end 
                           of lstAllUsers.</li>
          <li> Command button cmdWait: To suspend the selected user in lstAllUsers. No effect 
                              if the user is waiting or terminated, or no user is selected.</li>
          <li> Command button cmdResume: To resume the selected user in lstAllUsers. No effect 
                                 if no user is selected or the selected user is not waiting.</li>
          <li> Command button cmdTerminate: To terminate the selected user in lstAllUsers. The 
                                    user is not removed from lstAllUsers. No effect if the user is terminated 
                                    already or no user is selected.</li>
          <li> Command button cmdExit: To terminate all users, but wait until all users are 
                                       terminated gracefully. A messagebox is displayed to ask if the person running 
                                       the program wants to exit. If the answer if yes, the program will be 
                                       terminated; otherwise, new users can be created to do transactions.</li>
          </ul></li>
     <li> The main program keeps an integer variable for the balance, whose intial value is 1000.
                                    After each transaction, the balance should be modified and re-displayed 
                                    immediately.</li>
     <li> The main program also keeps the total transactions by all users. After a user 
                                    is terminated, the user will report its total amount of transactions, and this 
                                    amount should be used to modify the total transactions and the modified value 
                                    should be displayed immediately.</li>
<!--
Only the main thread updates the shared values and disaplys them.
Locks are not needed.

     <li> Several users may report transactions or total trasactions at the same time, 
                                    and mutual exclusion must be enforced. </li>
     <li> Some useful variables:
<pre>
   Dim AllUsers As New ArrayList              'To keep all users
   Dim TotalBalance As Integer                  
   Dim TotalTransactions As Integer

   'To control the Reading/Writing of TotalBalance
   Dim BalanceLock As New ReaderWriterLock      
   
   'To control the update of txtLog
   Dim LogLock As New ReaderWriterLock          

   'To control the update of lstAllUsers
   Dim MsgLock As New ReaderWriterLock          

   'To control the update of TotalTransactions
   Dim TransactionLock As New ReaderWriterLock
</pre></li>
-->

</ol>

<!--
Same thread: no lock needed
Another ReaderWriterLock object should be used for the total transactions.
-->

<h4>Submission </h4>
<ol>
<li> You must do the program in groups, the same groups as for the project. Each group will decide if the group
     should be divided into two subgroups for the program. </li>
<li> You must sign a group sheet by noon, Friday, April 20. </li>
<li> Use the UserName of one student to name the solution folder. </li>
<li> Drop your solution folder to folder Prog7 of the class DROP folder by 11 PM, Thursday, April 26, 2007. </li>
<li> You must send an email to the instructor by 11 PM, Thursday, April 26, 2007.
     <br> 
     Answer the following three questions in the email (1 is the lowest score and 5 is the highest score):
     <ol>
     <li> What do you think of your partner's programming skill? (1 to 5) </li>
     <li> What do you think of your partner's contribution to the program? (1 to 5) </li>
     <li> Do you want to work with your partner in the future? (1 to 5) </li>
     </ol>
     </li>
<li> You may lose up to two points for incorrect submission, including missing email and incorrect folder name. </li>
<li> You must follow the programming rules, and you may lose up to 2 points on style. </li>
</ol>

</body></html>